<?php

namespace Admin\Controller;

class MessageboxController extends AdminbaseController {

    function _initialize() {
        parent::_initialize();
        $this->currentModel = D("Messagebox");
    }

    //首页
    public function index() {

        $requestMap = $this->convertRequestToMap();
        $this->assign("begin_time", $_REQUEST["mb#create_time-egt"]);
        $this->assign("end_time", $_REQUEST["mb#create_time-elt"]);
        $this->assign("product_id", $_REQUEST["mb#product_id"]);

        //分页
        $count = $this->currentModel->getListCount($requestMap);

        $page = $this->page($count, C("DEFAULT_PAGE_SIZE"));

        //获取messagebox列表
        $messageboxList = $this->currentModel->queryForList($page, $requestMap);

        foreach( $messageboxList as &$item) {
            $category = D("Category")->queryById($item["category_id"]);
            $map["customer_id"] = $item["customer_id"];
            $map["product_id"] = $item["product_id"];
            if($category["is_finished"] == C("DEFAULT_YN.YES")) {
                $item["count"] = D("Order")->where($map)->count();
            }else{
                $item["count"] = D("Booking")->where($map)->count();
            }
        }

        $this->assign("defaultEnum", C("DEFAULT_YN"));
        $this->assign("statusEnum", C("MESSAGEBOX_STATUS"));
        $this->assign('messageboxList', $messageboxList);
        $this->assign("page", $page->show('Admin', $this->parameter));
        $this->display();

    }

    public function viewOrder() {

        //分页
        //$count = $this->currentModel->getListCount($requestMap);
        //$page = $this->page($count, C("DEFAULT_PAGE_SIZE"));

        //获取order列表
        $category = D("Category")->queryById($_REQUEST["category_id"]);

        if($category["is_finished"] == C("DEFAULT_YN.YES")) {
            $map["o.customer_id"] = $_REQUEST["customer_id"];
            $map["o.product_id"] = $_REQUEST["product_id"];
            $orderList = D("Order")->queryForList(null, $map);
        }else{
            $map["b.status"] = 1;
            $map["b.customer_id"] = $_REQUEST["customer_id"];
            $map["b.product_id"] = $_REQUEST["product_id"];
            $orderList = D("Booking")->queryForList(null, $map);
        }
        $this->assign("category", $category);
        $this->assign("defaultEnum", C("DEFAULT_YN"));
        $this->assign('bookingTypeEnum', C("BOOKING_TYPE"));
        $this->assign("statusEnum", C("ORDER_STATUS"));
        $this->assign('orderList', $orderList);
        $this->display();

    }

    public function viewNotify() {
        $messagebox = $this->currentModel->queryById($_REQUEST["id"]);
        $messagebox["name"] = $_REQUEST["name"];
        $messagebox["doctor"] = $_REQUEST["doctor"];
        $messagebox["hospital"] = $_REQUEST["hospital"];
        $messagebox["product"] = $_REQUEST["product"];
        $messagebox["count"] = $_REQUEST["count"];
        $this->assign("messagebox", $messagebox);
        $this->display();

    }

    public function notify()
    {

        $id = $_REQUEST["id"];

        $messagebox = $this->currentModel->queryById($id);
        $messagebox["status"] = C("MESSAGEBOX_STATUS.END");


        $company = D("Company")->queryById($_REQUEST["companyId"]);
        $customer = D("Customer")->queryById($company["customer_id"]);
        if($messagebox["notified"] != "")
            $messagebox["notified"] = $messagebox["notified"].",";
        $messagebox["notified"] = $messagebox["notified"].$customer["nickname"];

        //推送留言信息到销售
        $users_obj= M("admin");
        $id=$_SESSION['ADMIN_ID'];
        $user=$users_obj->where("user_id=$id")->find();
        $notifyData["openid"] = $customer["openid"];
        $notifyData["first"] = "收到患者留言，请您联系该患者。——"."管理员【".$user["admin_name"]."】";
        $notifyData["keyword1"] = $_REQUEST["name"]."，城市【".$_REQUEST["city"]."】";
        $notifyData["keyword2"] = $_REQUEST["number"];
        $notifyData["keyword3"] = $_REQUEST["time"];
        $notifyData["keyword4"] = $_REQUEST["messageContent"];
        $notifyData["remark"] = "咨询产品：【".$_REQUEST["product"]."】，曾购买 ".$_REQUEST["count"]."件，所属医院：".$_REQUEST["hospital"]."，主治医生：".$_REQUEST["doctor"]."。";
        messageboxNotify($notifyData);
        $this->currentModel->updateInfo($messagebox);

        $this->success("已通知销售：", U("Messagebox/index"));


    }

    public function groupnotify() {
        $requestMap = $this->convertRequestToMap();
//        $count = D("Groupnotify")->getListCount($requestMap);
//        $page = $this->page($count, C("DEFAULT_PAGE_SIZE"));
        $list = D("Groupnotify")->queryForList(null, $requestMap);
        $this->assign("list", $list);
        $this->assign("WEB_HOST", C('WEB_HOST'));
        $this->display();

    }

    public function groupnotifyPost()
    {
        $groupName = "";
        $groupId = $_REQUEST["group"];
        switch($groupId) {
            case 0:
                if(!isNullOrEmpty($_REQUEST["tag"])){
                    $customer = D("Customer")->where(array('tag'=>$_REQUEST["tag"]))->field("openid")->select();
                }else{
                    $customer = D("Customer")->field("openid")->select();
                }
                $customer = D("Customer")->field("openid")->select();
                $openidArray = array();
                foreach ($customer as $value) {
                    array_push($openidArray, $value["openid"]);
                }
                break;
            case 1:
                $enterprise = D("Enterprise")->where(array('enterprise_id'=>$_REQUEST["enterpriseId"]))->find();
                $groupName = $enterprise["enterprise_name"];
                $roleId = $_REQUEST["roleId"];
                if($roleId == 0){//全体员工
                    $adminRoleArr = explode(',', $enterprise["role_list"]);
                    $groupName .= "(全体)";
                }else{
                    $role = D("AdminRole")->where(array('id'=>$roleId))->find();
                    $adminRoleArr = array($roleId);
                    $groupName .= "(" . $role["name"] . ")";
                }
                $openidArray = getNotifyArr($adminRoleArr, $enterprise["enterprise_code"]);
                break;
            case 2:
                break;
        }
        $userId=$_SESSION['ADMIN_ID'];
        $user=M("admin")->where("user_id=$userId")->find();
        $id = D("Groupnotify")->add(array(
            'create_time' => getCurrentDate(),
            'update_id' => $user["employee_id"],
            'group' => $_REQUEST["group"],
            'url' => $_REQUEST["url"],
            'first' => $_REQUEST["first"],
            'keyword1' => $_REQUEST["keyword1"],
            'keyword3' => $_REQUEST["keyword3"],
            'remark' => $_REQUEST["remark"],
        ));
        //推送留言
//        $users_obj= M("admin");
//        $id=$_SESSION['ADMIN_ID'];
//        $user=$users_obj->where("user_id=$id")->find();
        if($groupId == 0 || $groupId == 1){
            $notifyData["url"] = $_REQUEST["url"];
            $notifyData["first"] = $_REQUEST["first"];
            $notifyData["keyword1"] = $_REQUEST["keyword1"];
            $notifyData["keyword2"] = getCurrentDate();
            $notifyData["keyword3"] = $_REQUEST["keyword3"];
            $notifyData["remark"] = $_REQUEST["remark"];
            $openidArray = array_unique($openidArray);
            foreach ($openidArray as $openid) {
                $notifyData["openid"] = $openid;
                groupNotify($notifyData);
            }
        }
        if($groupId == 2){
            $notifyData["url"] = "http://".C('WEB_HOST')."/wechat/clubfoot/myIcoin";
            $notifyData["first"] = "IOWA支具穿戴打卡爱币积分奖励";

            $notifyData["keyword2"] = $_REQUEST["month"]." 月打卡";
//            $notifyData["keyword3"] = $_REQUEST["month"]." 月打卡";
//            $notifyData["keyword4"] = "＋" . $_REQUEST["point"];
//            $notifyData["remark"] = $_REQUEST["remark"];
            $map["update_month"] = $_REQUEST["month"];
            $customerList = D("Customer")->where($map)->field("nickname,openid,point")->select();
            $customerList = array_unique($customerList);
            foreach ($customerList as $customer) {
                $notifyData["keyword1"] = "微信用户：".$customer["nickname"]."，积分＋50";
                $notifyData["remark"] = "当前积分：".$customer["point"];
                $notifyData["openid"] = $customer["openid"];
                pointChangeNotify($notifyData);
            }
        }

        $this->success("已通知给 ".$groupName."：", U("messagebox/groupnotify"));
    }
}